package com.ddddddl.zhangben.ui.widget;

import com.ddddddl.zhangben.R;

import android.content.Context;
import android.text.Editable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AmountKeybroadView extends LinearLayout implements android.view.View.OnClickListener {
	
	private EditText mEditText;

	public AmountKeybroadView(Context context) {
		super(context);
		init();

	}

	public AmountKeybroadView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	private void init() {
		LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.amount_keybroad, this, true);
		TextView number1 = (TextView) findViewById(R.id.number1);
		number1.setOnClickListener(this);
		TextView number2 = (TextView) findViewById(R.id.number2);
		number2.setOnClickListener(this);
		TextView number3 = (TextView) findViewById(R.id.number3);
		number3.setOnClickListener(this);
		TextView number4 = (TextView) findViewById(R.id.number4);
		number4.setOnClickListener(this);
		TextView number5 = (TextView) findViewById(R.id.number5);
		number5.setOnClickListener(this);
		TextView number6 = (TextView) findViewById(R.id.number6);
		number6.setOnClickListener(this);
		TextView number7 = (TextView) findViewById(R.id.number7);
		number7.setOnClickListener(this);
		TextView number8 = (TextView) findViewById(R.id.number8);
		number8.setOnClickListener(this);
		TextView number9 = (TextView) findViewById(R.id.number9);
		number9.setOnClickListener(this);
		TextView number0 = (TextView) findViewById(R.id.number0);
		number0.setOnClickListener(this);
		TextView back = (TextView) findViewById(R.id.back);
		back.setOnClickListener(this);
		TextView dot = (TextView) findViewById(R.id.dot);
		dot.setOnClickListener(this);
	}
	
	public void setEdittext(EditText editText) {
		mEditText = editText;
	}

	@Override
	public void onClick(View v) {
		if (mEditText != null) {
			Editable text = mEditText.getText();
			switch (v.getId()) {
			case R.id.number1:
			case R.id.number2:
			case R.id.number3:
			case R.id.number4:
			case R.id.number5:
			case R.id.number6:
			case R.id.number7:
			case R.id.number8:
			case R.id.number9:
			case R.id.number0:
				TextView number = (TextView) v;
				mEditText.append(number.getText());
				break;
			case R.id.back:
				int length = text.length();
				mEditText.setText(text.subSequence(0, length - 1));
				break;
			case R.id.dot:
				TextView dot = (TextView) v;
				if (text.toString().indexOf(".") < 0) {
					mEditText.append(dot.getText());
				}
				break;
			default:
				break;
			}
		}
	}

}
